package org.blogwu.learn.multithread.chapter2.demo4;


/**
 * 这个 案例是:当一个线程进入临界区时,吧他suspend,这是会释放锁,另一个线程获取锁进入临界区.当吧两个线程继续执行的
 * 时候就陷入死锁了
 *
 * @author hyf
 * @date 2018-09-23
 **/
public class Main2 {
    public static void main(String[] args) throws InterruptedException {
        SuspendThreadTest suspendThreadTest1 = new SuspendThreadTest("s1");
        SuspendThreadTest suspendThreadTest2 = new SuspendThreadTest("s2");
        suspendThreadTest2.start();
        Thread.sleep(1000);
        suspendThreadTest1.start();
        suspendThreadTest2.resume();
        suspendThreadTest1.resume();
//       suspendThreadTest2.resume();

        suspendThreadTest1.join();
        suspendThreadTest2.join();
    }
}

class SuspendThreadTest extends Thread {
    static Object o1 = new Object();

    public SuspendThreadTest(String name) {
        super(name);
    }

    @Override
    public void run() {
        synchronized (o1) {
            System.out.println("in " + getName());
            this.suspend();
        }
    }

}
